<?php
namespace App\Logics;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\LogNotifyAlipayModel;
use App\Models\LogXxhfModel;
use App\Models\LogXyjhModel;
use App\Models\LogXcxModel;
use App\Models\AutoLogModel;

//逻辑处理
class LogLogic extends BaseLogic {


    public function __construct()
    {
        parent::__construct();

    }

    public   function  h5paylog(array $para){
        $page = isset($para['page'])?$para['page']:1;
        $per_page = isset($para['per_page'])?$para['per_page']:20;
        $order_stages_id=isset($para['order_stages_id'])?$para['order_stages_id']:0;

        $where = $searchWhere = [];
        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }
        if($order_stages_id){
            $where['order_stages_id'] = $order_stages_id;
            $searchWhere[]=['order_stages_id','like',$order_stages_id.'%'];
        }



        $model =  LogNotifyAlipayModel::where($searchWhere);
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');
        $url=config('sys.img_url').'/';

        if(isset($where['per_page']))unset($where['per_page']);
        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url
        ];
        return $arr;

    }

    public function xyjhlog(array $para){
        $page = isset($para['page'])?$para['page']:1;
        $per_page = isset($para['per_page'])?$para['per_page']:20;
        $out_order_no=isset($para['out_order_no'])?$para['out_order_no']:'';

        $where = $searchWhere = [];
        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }
        if($out_order_no){
            $where['out_order_no'] = $out_order_no;
            $searchWhere[]=['out_order_no','like',$out_order_no.'%'];
        }

        $model = LogXyjhModel::where($searchWhere);
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');
        $url=config('sys.img_url').'/';

        if(isset($where['per_page']))unset($where['per_page']);
        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url
        ];
        return $arr;
    }

    public function xcxlog(array $para){
        $page = isset($para['page'])?$para['page']:1;
        $per_page = isset($para['per_page'])?$para['per_page']:20;
        $out_order_no=isset($para['out_order_no'])?$para['out_order_no']:'';

        $where = $searchWhere = [];
        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }
        if($out_order_no){
            $where['out_order_no'] = $out_order_no;
            $searchWhere[]=['out_order_no','like',$out_order_no.'%'];
        }

        $model = LogXcxModel::where($searchWhere);
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');
        $url=config('sys.img_url').'/';

        if(isset($where['per_page']))unset($where['per_page']);
        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url
        ];
        return $arr;
    }

    public function autolog(array $para){
        $page = isset($para['page'])?$para['page']:1;
        $per_page = isset($para['per_page'])?$para['per_page']:20;
        $out_order_no=isset($para['out_order_no'])?$para['out_order_no']:'';
        $where = $searchWhere = [];
        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }
        if($out_order_no){
            $where['out_order_no'] = $out_order_no;
            $searchWhere[]=['out_order_no','like',$out_order_no.'%'];
        }


        $type=['1'=>'确认收货', '2'=>'小程序代扣', '3'=>'逾期',  '4'=>'短信提醒', '5'=>'逾期短信'];
        $model = AutoLogModel::where($searchWhere);
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        foreach($data['data'] as $k=>$v){
            $data['data'][$k]['type']=isset($type[$v['type']])?$type[$v['type']]:'';
            $data['data'][$k]['status']=$v['status']==1?'完成':'程序开始';
        }
        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');
        $url=config('sys.img_url').'/';

        if(isset($where['per_page']))unset($where['per_page']);
        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url
        ];
        return $arr;
    }

    public function xxhflog(array $para){
        $page = isset($para['page'])?$para['page']:1;
        $per_page = isset($para['per_page'])?$para['per_page']:20;
        $order_id=isset($para['order_id'])?$para['order_id']:'';

        $where = $searchWhere = [];
        if ($per_page != 20) {
            $where['per_page'] = $per_page;
        }
        if($order_id){
            $where['order_id'] = $order_id;
            $searchWhere[]=['order_id','like',$order_id.'%'];
        }


        $model = LogXxhfModel::where($searchWhere);
        $data = $model->paginate($per_page, ['*'], 'page', $page);
        $data = ob_to_array($data);
        $data['page_list'] = $this->page($data['last_page'], $data['current_page']);
        $data['up_page'] = ($data['current_page'] - 1) > 0 ? $data['current_page'] - 1 : 1;
        $data['next_page'] = ($data['current_page'] + 1) > $data['last_page'] ? $data['last_page'] : $data['current_page'] + 1;
        $url = url()->current() . '?&';
        foreach ($where as $k => $v) {
            $url .= "$k=$v&";
        }
        $data['page_url'] = trim($url, '&');
        $url=config('sys.img_url').'/';

        if(isset($where['per_page']))unset($where['per_page']);
        $arr=[
            'data'=>$data,
            'where'=>$where,
            'url'=>$url
        ];
        return $arr;
    }

    public static function getOne(array $where=[],array $fields=['*']){
        $data= LogNotifyAlipayModel::where($where)->select($fields)->first();
        return ob_to_array($data);
    }






}
